<?php
/*
error_reporting(E_ALL);
header(500);
exit;
*/
date_default_timezone_set('GMT');

// TODO: referer check

if ($_REQUEST['action']) {
	require_once('config.php');



	$qry = '';
	$results = array();
	$op_id = 0;
	switch ($_REQUEST['action']) {
		/////
		case 'saveOp':
			$op_id = saveOp();
			$results = getOp($_REQUEST['owner_id'], $op_id);
			for ($i=0;$i<count($results);$i++) {
				$results[$i]['Members'] = getMember($results[$i]['id']);
				$results[$i]['Hauls'] = getHaul($results[$i]['id']);
				$results[$i]['Comments'] = getComment($results[$i]['id']);
			}
			echo json_encode($results);
			break;

		case 'getOp':
			$results = getOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			for ($i=0;$i<count($results);$i++) {
				$active_out = 0;
				$active_part = 0;
				$results[$i]['Members'] = getMember($results[$i]['id']);
				$results[$i]['Hauls'] = getHaul($results[$i]['id']);
				$results[$i]['Comments'] = getComment($results[$i]['id']);
/*
// TODO: get total runningtime
var_dump($results[$i]['Members']);
				for ($j=0;$j<count($results[$i]['Members']);$j++) {
var_dump($results[$i]['Members'][$j]);
//echo $results[$i]['Members'][$j]['date_joined']."<BR/>\n";
//echo $results[$i]['Members'][$j]['date_parted']."<BR/>\n";

					if ($results[$i]['Members'][$j]['date_joined'] > $active_part) {
						$active_out += $results[$i]['Members'][$j]['date_joined']-$active_part;
						$active_part = $results[$i]['Members'][$j]['date_parted'];
					}
				}

				$results[$i]['Totals']['time_running'] = $active_part-$active_out;
*/
			}

			echo json_encode($results);
			break;

		case 'finishOp':
			finishOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			$results = getOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			for ($i=0;$i<count($results);$i++) {
				$results[$i]['Members'] = getMember($results[$i]['id']);
				$results[$i]['Hauls'] = getHaul($results[$i]['id']);
				$results[$i]['Comments'] = getComment($results[$i]['id']);
			}
			echo json_encode($results);
			break;

		case 'paidOp':
			paidOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			$results = getOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			for ($i=0;$i<count($results);$i++) {
				$results[$i]['Members'] = getMember($results[$i]['id']);
				$results[$i]['Hauls'] = getHaul($results[$i]['id']);
				$results[$i]['Comments'] = getComment($results[$i]['id']);
			}
			echo json_encode($results);
			break;

		case 'delOp':
			delOp($_REQUEST['owner_id'], $_REQUEST['op_id']);
			break;
		/////




		/////
		case 'sendComment':
			$member_id = sendComment($_REQUEST['op_id'], $_REQUEST['user_id'],$_REQUEST['comment']);
			$results['Comments'] = getComment($_REQUEST['op_id']);
			echo json_encode($results);
			break;


		/////
		case 'joinMember':
			$member_id = joinMember($_REQUEST['op_id'], $_REQUEST['user_id'],$_REQUEST['displayname'],$_REQUEST['profile_url']);
			$results['Members'] = getMember($_REQUEST['op_id']);
			echo json_encode($results);
			break;

		case 'partMember':
			$member_id = partMember($_REQUEST['op_id'], $_REQUEST['user_id']);
			$results['Members'] = getMember($_REQUEST['op_id']);
			echo json_encode($results);
			break;

		case 'getMember':
			$results = getMember($_REQUEST['op_id'], $_REQUEST['user_id']);
			echo json_encode($results);
			break;

		case 'delMember':
			delMember($_REQUEST['op_id'], $_REQUEST['user_id']);
			break;




		case 'saveShares':
			$member_id = saveShares($_REQUEST['op_id'], $_REQUEST['user_id'], $_REQUEST['shares']);
			$results['Members'] = getMember($_REQUEST['op_id']);
			echo json_encode($results);
			break;
		/////


		/////
		case 'savePayout':
			break;
		case 'getPayout':
			break;
		case 'delPayout':
			break;
		/////


		/////
		case 'saveHaul':
			$haul_id = saveHaul($_REQUEST['op_id'], $_REQUEST['user_id']);
			$results['id'] = $_REQUEST['op_id'];
			$results['Hauls'] = getHaul($_REQUEST['op_id']);
			echo json_encode($results);
			break;

		case 'getHaul':
			$results = getHaul($_REQUEST['op_id'], $_REQUEST['haul_id']);
			_toarray($results);
			break;

		case 'delHaul':
			delHaul($_REQUEST['haul_id']);
			$results['id'] = $_REQUEST['op_id'];
			$results['Hauls'] = getHaul($_REQUEST['op_id']);
			echo json_encode($results);
			break;
		/////


		/////
		case 'saveGroup':
			saveGroup($_REQUEST['group_id'],$_REQUEST['displayname'],$_REQUEST['url'],$_REQUEST['userids']);
			break;
		case 'getGroup':
			$results = getGroup($_REQUEST['user_id']);
			echo json_encode($results);
			break;
		/////



		/////
		case 'saveBuyer':
			$buyer_id = saveBuyer($_REQUEST['user_id']);
			echo json_encode($buyer_id);
			break;

		case 'rateBuyer':
			$rating_id = rateBuyer($_REQUEST['buyer_id'], $_REQUEST['user_id'], $_REQUEST['rateup']);
			$output = array();
			$output['buyer_id'] = $_REQUEST['buyer_id'];
			$output['rateup'] = $_REQUEST['rateup'];
			echo json_encode($output);
			break;

		case 'getBuyer':
			$results = getBuyer($_REQUEST['user_id']);
			echo json_encode($results);
			break;

		case 'delBuyer':
			delBuyer($_REQUEST['user_id']);
			break;
		/////



	}

	mysql_close();
}











////////////
// Hauls //
//////////

function saveHaul($op_id, $userid) {
	$fields = _getFields('mm_hauls');


	$qry = '';
	if (!empty($_REQUEST['haul_id'])) {
		$qry .= 'UPDATE mm_hauls SET ';
		$cnt = 0;
		foreach ($fields as $field) {
			if (empty($_REQUEST[$field])) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= $field."='".mysql_escape_string($_REQUEST[$field])."'";
			$cnt++;
		}
		$qry .= ' WHERE id = '.$_REQUEST['haul_id'];

	} else {
		$qry .= 'INSERT INTO mm_hauls (';
		$cnt = 0;
		foreach ($fields as $field) {
			if (empty($_REQUEST[$field])) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= $field;
			$cnt++;
		}
		$qry .= ', date_hauled';
		$qry .= ') VALUES(';
		$cnt = 0;
		foreach ($fields as $field) {
			if (empty($_REQUEST[$field])) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= "'".mysql_escape_string($_REQUEST[$field])."'";
			$cnt++;
		}
		$qry .= ",'".gmdate('Y-m-d H:i:s')."'";
		$qry .= ')';
	}

//	echo $qry;
	$result = mysql_query($qry);

	return mysql_insert_id();
}

function getHaul($op_id=null, $haul_id=null) {
	$qry = 'SELECT * FROM mm_hauls WHERE ';
	if (!empty($haul_id)) {
		$qry .= 'id='.$haul_id.' AND ';
	}
	if (!empty($op_id)) {
		$qry .= 'op_id='.$op_id.' AND ';
	}
	$qry .= 'date_deleted Is Null ORDER BY date_hauled DESC';
	//echo $qry;
	$results = mysql_query($qry);

	return _toarray($results);
}

function delHaul($haul_id=null) {
	if (empty($haul_id)) return;
//	$qry = 'DELETE FROM mm_hauls WHERE id='.$haul_id;
	$qry .= "UPDATE mm_hauls SET date_deleted = '".gmdate('Y-m-d H:i:s')."' ";
	$qry .= "WHERE (id=".$haul_id.")";
	//echo $qry;
	$results = mysql_query($qry);

	return $results;
}




///////////////
// Comments //
/////////////

function sendComment($op_id, $userid, $comment) {
	if (empty($op_id)) return;
	if (empty($userid)) return;
	if (empty($comment)) return;

	$qry = '';
	$qry .= 'INSERT INTO mm_comments (user_id, op_id, message, date_created, date_modified) VALUES(';
	$qry .= "'".mysql_escape_string($userid)."',";
	$qry .= $op_id.",";
	$qry .= "'".mysql_escape_string($comment)."',";
	$qry .= "'".gmdate('Y-m-d H:i:s')."',";
	$qry .= "'".gmdate('Y-m-d H:i:s')."'";
	$qry .= ');';
//	echo $qry;
	$result = mysql_query($qry);
	return mysql_insert_id();
}


function getComment($op_id=null) {
	$qry = 'SELECT * FROM mm_comments WHERE ';
	if (!empty($op_id)) {
		$qry .= 'op_id='.$op_id.' AND ';
	}
	$qry .= '1=1 ORDER BY date_created DESC';
//	echo $qry;
	$results = mysql_query($qry);

	return _toarray($results);
}


//////////////
// Members //
////////////

function joinMember($op_id, $userid, $displayname, $profile_url) {
	if (empty($op_id)) return;
	if (empty($userid)) return;
	if (empty($displayname)) return;

	partMember($op_id, $userid);

	$qry = '';
	$qry .= 'INSERT INTO mm_members (user_id, op_id, displayname, profile_url, date_joined) VALUES(';
	$qry .= "'".mysql_escape_string($userid)."',";
	$qry .= $op_id.",";
	$qry .= "'".mysql_escape_string($displayname)."',";
	$qry .= "'".mysql_escape_string($profile_url)."',";
	$qry .= "'".gmdate('Y-m-d H:i:s')."'";
	$qry .= ');';
	//echo $qry;
	$result = mysql_query($qry);
	return mysql_insert_id();
}

function partMember($op_id, $userid) {
	if (empty($op_id)) return;
	if (empty($userid)) return;

	$qry = '';
	$qry .= "UPDATE mm_members SET date_parted = '".gmdate('Y-m-d H:i:s')."' ";
	$qry .= "WHERE (user_id='".$userid."' AND op_id=".$op_id." AND date_parted Is Null)";
	//echo $qry;
	$result = mysql_query($qry);
	return $op_id;
}

function saveShares($op_id, $userid, $shares) {
	if (empty($op_id)) return;
	if (empty($userid)) return;
	if (empty($shares)) $shares = 0;

	$qry = '';
	$qry .= "UPDATE mm_members SET shares = ".mysql_escape_string($shares)." ";
	$qry .= "WHERE (user_id='".$userid."' AND op_id=".$op_id.")";
	//echo $qry;
	$result = mysql_query($qry);
	return $op_id;
}


function getMember($op_id=null, $userid=null) {
	$qry = 'SELECT * FROM mm_members WHERE ';
/*
	if (!empty($memberid)) {
		$qry .= 'id='.$memberid.' AND ';
	}
*/
	if (!empty($op_id)) {
		$qry .= 'op_id='.$op_id.' AND ';
	}
	if (!empty($user_id)) {
		$qry .= 'user_id='.$user_id.' AND ';
	}
	$qry .= '1=1 ORDER BY user_id, date_parted DESC';
//echo $qry;
	$results = mysql_query($qry);
	$data = _toarray($results);

	$active_part = 0;
	$active_out = 0;
	$total_running = 0;

	$total = 0;
	$total_timeshare = 0;
	$output = array();
	foreach ($data as $item) {
		$output[$item['user_id']]['displayname'] = $item['displayname'];
		$output[$item['user_id']]['profile_url'] = $item['profile_url'];
		$output[$item['user_id']]['shares'] = $item['shares'];
		if (!empty($item['date_parted'])) {
			$datepart = strtotime($item['date_parted'], 0);
			$output[$item['user_id']]['status'] = 'Inactive';
		} else {
			$datepart = time();
			$output[$item['user_id']]['status'] = 'Active';
		}

		$datejoin = strtotime($item['date_joined'], 0);

/*
		if ($output[$item['user_id']]['date_joined'] > $active_part) {
			$active_out += $output[$item['user_id']]['date_joined']-active_part;
			$active_part = $output[$item['user_id']]['date_parted'];
		}
*/
		if (!$output[$item['user_id']]['time']) $output[$item['user_id']]['time'] = 0;
		if (!$output[$item['user_id']]['timeshare']) $output[$item['user_id']]['timeshare'] = 0;
		$output[$item['user_id']]['time'] += ($datepart - $datejoin);
		$output[$item['user_id']]['timeshare'] += ($datepart - $datejoin)*$item['shares'];

		$total += ($datepart - $datejoin);
		$total_timeshare += ($datepart - $datejoin)*$item['shares'];
	}
	foreach ($output as $userid=>$value) {
		if ($total > 0) {
//			$output[$userid]['perc'] = $output[$userid]['time'] / $total;
			$output[$userid]['perc'] = $output[$userid]['timeshare'] / $total_timeshare;
		} else {
			$output[$userid]['perc'] = 0;
		}
		$output[$userid]['op_id'] = $op_id;
	}
//	$output['Totals'] = $active_part-$active_out;

	return $output;
}



function delMember($op_id, $userid) {
	if (empty($op_id)) return;
	if (empty($userid)) return;

	$qry = '';
	$qry .= "DELETE FROM mm_members WHERE (user_id='".$userid."' AND op_id=".$op_id.")";
	//echo $qry;
	$result = mysql_query($qry);
	return $result;
}



//////////
// Ops //
////////

function saveOp() {
	$fields = _getFields('mm_ops');

	$qry = '';
	if (!empty($_REQUEST['op_id'])) {
		$qry .= 'UPDATE mm_ops SET ';
		$cnt = 0;
		foreach ($fields as $field) {
			$input = $_REQUEST[$field];
			if (empty($input)) {
				switch ($field) {
					case 'system':
						$input = 'Classified';
						break;
					case 'region':
						$input = 'Universal';
						break;
					case 'date_started':
						$input = gmdate('Y-m-d H:i:s');
						break;
				}
			}

			if (empty($input)) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= $field."='".mysql_escape_string($input)."'";
			$cnt++;
		}
		$qry .= ' WHERE id = '.$_REQUEST['op_id'];

	} else {
		$qry .= 'INSERT INTO mm_ops (';
		$cnt = 0;
		foreach ($fields as $field) {
			$input = $_REQUEST[$field];
			if (empty($input)) {
				switch ($field) {
					case 'system':
						$input = 'Classified';
						break;
					case 'region':
						$input = 'Universal';
						break;
					case 'date_started':
						$input = gmdate('Y-m-d H:i:s');
						break;
				}
			}
			if (empty($input)) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= $field;
			$cnt++;
		}
		$qry .= ') VALUES(';
		$cnt = 0;
		foreach ($fields as $field) {
			$input = $_REQUEST[$field];
			if (empty($input)) {
				switch ($field) {
					case 'system':
						$input = 'Classified';
						break;
					case 'region':
						$input = 'Universal';
						break;
					case 'date_started':
						$input = gmdate('Y-m-d H:i:s');
						break;
				}
			}
			if (empty($input)) continue;
			if ($cnt > 0) $qry .= ',';
			$qry .= "'".mysql_escape_string($input)."'";
			$cnt++;
		}
		$qry .= ')';
	}
	//echo $qry;
	$result = mysql_query($qry);

	if ($_REQUEST['op_id']) {
		return $_REQUEST['op_id'];
	} else {
		return mysql_insert_id();
	}
}

function getOp($owner_id=null, $op_id=null) {
	$qry = 'SELECT * FROM mm_ops WHERE ';
	if (!empty($op_id)) {
		$qry .= 'id='.$op_id.' AND ';
	}
	if (!empty($owner_id)) {
//		$qry .= "owner_id IN '".mysql_escape_string($owner_id)."' OR ";
		$owners = explode(',',$owner_id);
		$qry .= "owner_id IN ";
		$qry .= "(";
		$cnt = 0;
		foreach ($owners as $owner) {
			if ($cnt > 0) $qry .= ",";
			$qry .= "'".mysql_escape_string($owner)."'";
			$cnt++;
		}
		$qry .= ") AND ";
	}
	$qry .= 'date_deleted Is Null ORDER BY date_started DESC';
//	echo $qry;
	$results = mysql_query($qry);

	return _toarray($results);
}

function finishOp($owner_id, $op_id) {
	$qry = '';
	if (!empty($op_id)) {
		$qry .= "UPDATE mm_ops SET date_closed = '".gmdate('Y-m-d H:i:s')."' ";
		$qry .= "WHERE (id=".$op_id." AND owner_id='".mysql_escape_string($owner_id)."' AND date_closed Is Null)";
//		echo $qry;
		$result = mysql_query($qry);

		$qry = '';
		$qry .= "UPDATE mm_members SET date_parted = '".gmdate('Y-m-d H:i:s')."' ";
		$qry .= "WHERE (op_id=".$op_id." AND date_parted Is Null)";
		//echo $qry;
		$result = mysql_query($qry);

		return $op_id;
	}
}


function paidOp($owner_id, $op_id) {
	$qry = '';
	if (!empty($op_id)) {
		$qry .= "UPDATE mm_ops SET date_paid = '".gmdate('Y-m-d H:i:s')."' ";
		$qry .= "WHERE (id=".$op_id." AND owner_id='".mysql_escape_string($owner_id)."' AND date_paid Is Null)";
//		echo $qry;
		$result = mysql_query($qry);
		return $op_id;
	}
}

function delOp($owner_id, $op_id) {
//	$qry = "DELETE FROM mm_ops WHERE id=".$op_id." AND owner_id='".mysql_escape_string($owner_id)."'";
	$qry .= "UPDATE mm_ops SET date_deleted = '".gmdate('Y-m-d H:i:s')."' ";
	$qry .= "WHERE (id=".$op_id." AND owner_id='".mysql_escape_string($owner_id)."')";
	$results = mysql_query($qry);
	return $results;
}


function saveGroup($group_id,$displayname,$url,$userids) {
	if (!empty($group_id) && !empty($displayname) && !empty($userids)) {
		$qry = "INSERT INTO mm_groups (group_id, displayname, url) VALUES ('".mysql_escape_string($group_id)."','".mysql_escape_string($displayname)."','".mysql_escape_string($url)."');";
		$results = mysql_query($qry);
	
		$qry = "SELECT id FROM mm_groups WHERE (group_id='".mysql_escape_string($group_id)."') LIMIT 1;";
		$results = mysql_query($qry);
		$groups = _toarray($results);
		$group_pk = $groups[0]['id'];
		if ($group_pk > 0) {
			$qry = "DELETE FROM mm_group_members WHERE group_id=".$group_pk.";";
			$results = mysql_query($qry);

			$users = explode(',',$userids);
			foreach ($users as $userid) {
				$qry = "INSERT INTO mm_group_members (group_id, user_id) VALUES(".$group_pk.",'".mysql_escape_string($userid)."');";
				$results = mysql_query($qry);
			}
		}
	}
	return true;
}

function getGroup($user_id) {
	if (!empty($user_id)) {
		$qry = "SELECT mm_groups.* FROM mm_group_members INNER JOIN mm_groups ON mm_group_members.group_id = mm_groups.id WHERE mm_group_members.user_id = '".$user_id."' ORDER BY mm_groups.displayname;";
		$results = mysql_query($qry);
		return _toarray($results);
	} else {
		return false;
	}	
}







//////////
// Buyers //
////////

function saveBuyer() {
	$fields = _getFields('mm_buyers');

	$qry .= "INSERT INTO mm_buyers (user_id) VALUES('".mysql_escape_string($_REQUEST['user_id'])."');";
	$result = mysql_query($qry);

	$qry = '';
	$qry .= 'UPDATE mm_buyers SET ';
	$cnt = 0;
	foreach ($fields as $field) {
		if ($field == 'id') continue;
		$input = $_REQUEST[$field];
		if (empty($input)) {
			switch ($field) {
				case 'system':
					$input = '';
					break;
				case 'region':
					$input = 'Universal';
					break;
				case 'date_created':
					$input = gmdate('Y-m-d H:i:s');
					break;
			}
		}

//		if (empty($input)) continue;
		if (empty($input) && substr($field,5) == 'flag_') $input = 0;
		if ($cnt > 0) $qry .= ',';
		$qry .= $field."='".mysql_escape_string($input)."'";
		$cnt++;
	}
	$qry .= " WHERE user_id = '".mysql_escape_string($_REQUEST['user_id'])."'";

//	echo $qry;
	$result = mysql_query($qry);

	if ($_REQUEST['user_id']) {
		return $_REQUEST['user_id'];
	} else {
		return mysql_insert_id();
	}
}

function getBuyer($buyer_id=null) {
	$qry = 'SELECT * FROM mm_buyers WHERE ';
	if (!empty($buyer_id)) {
		$qry .= "user_id='".mysql_escape_string($buyer_id)."' AND ";
	}
	$qry .= '(';
	$qry .= 'flag_mercoxit OR ';
	$qry .= 'flag_arkonor OR ';
	$qry .= 'flag_bistot OR ';
	$qry .= 'flag_spodumain OR ';
	$qry .= 'flag_crokite OR ';
	$qry .= 'flag_darkochre  OR ';
	$qry .= 'flag_gneiss OR ';
	$qry .= 'flag_hedbergite OR ';
	$qry .= 'flag_hemorphite OR ';
	$qry .= 'flag_jaspet OR ';
	$qry .= 'flag_kernite OR ';
	$qry .= 'flag_omber OR ';
	$qry .= 'flag_plagioclase OR ';
	$qry .= 'flag_pyroxeres OR ';
	$qry .= 'flag_scordite OR ';
	$qry .= 'flag_veldspar OR ';
	$qry .= 'flag_isogen OR ';
	$qry .= 'flag_megacyte OR ';
	$qry .= 'flag_mexallon OR ';
	$qry .= 'flag_morphite OR ';
	$qry .= 'flag_nocxium OR ';
	$qry .= 'flag_pyerite OR ';
	$qry .= 'flag_tritanium OR ';
	$qry .= 'flag_zydrine';
	$qry .= ') AND ';
	$qry .= '1=1 ORDER BY region, date_created DESC';
//	echo $qry;
	$results = mysql_query($qry);
	$buyers = _toarray($results);
	$cnt = 0;
	foreach ($buyers as $buyer) {
		$buyers[$cnt]['Rating'] = array();
		$buyers[$cnt]['Rating'] = getRating($buyer['user_id']);
//		$buyers[$cnt]['RatingViewers'] = getRating($buyer['user_id'],$_REQUEST['viewer_id']);
		$cnt++;
	}
	return $buyers;
}

function delBuyer($buyer_id) {
	$qry = "DELETE FROM mm_buyers WHERE user_id='".mysql_escape_string($buyer_id)."'";
	$results = mysql_query($qry);
	return $results;
}


function getRating($buyer_id) {
	$qry = "SELECT buyer_id, user_id, rateup FROM mm_buyer_ratings ";
	$qry .= "WHERE buyer_id = '".mysql_escape_string($buyer_id)."' ";

	//echo $qry;
	$res = mysql_query($qry);
	$results = _toarray($res);
	$pos = 0;
	$neg = 0;
	$total = 0;
	foreach ($results as $result) {
		if ($result['rateup']) {
			$pos++;
		} else {
			$neg++;
		}
		$total++;
	}
	$output = array();
	$output['pos'] = $pos;
	$output['neg'] = $neg;
	$output['total'] = $total;

	return $output;
}
function rateBuyer($buyer_id, $user_id, $rateup) {
	$qry .= 'INSERT INTO mm_buyer_ratings (';
	$qry .= 'buyer_id,user_id,rateup,date_created';
	$qry .= ') VALUES(';
	$qry .= "'".$buyer_id."','".$user_id."',".$rateup.",'".gmdate('Y-m-d H:i:s')."'";
	$qry .= ') ON DUPLICATE KEY UPDATE rateup = '.$rateup.';';
	//echo $qry;
	$result = mysql_query($qry);
	return $results;
}



///////////////
// Privates //
/////////////

function _getFields($table = 'mm_ops') {
	$qry = 'SELECT * FROM '.$table;
	$result = mysql_query($qry);
	$cols = mysql_num_fields($result);
	$fields = array();
	for ($i=0; $i<$cols; $i++) {
		array_push($fields, mysql_field_name($result,$i));
	}

	return $fields;
}

function _toarray($result) {
	$data = array();
	if ($result!=null) {
		while($row = mysql_fetch_assoc($result)) {
			$data[] = $row;
		}
	}
	return $data;
}
?>